﻿using System;
using System.Collections.Generic;
using System.Web;
using UniversityManagementSystem.Manager;

namespace UniversityManagementSystem.DAL
{
    [Serializable]
    public class Student
    {
        StudentManager manager = new StudentManager();
        public string RegistrationNo { get; set; }
        public string name { get; set; }

        public string email { get; set; }

        public string departmentName { get; set; }

        public string contactNo { get; set; }

        public string registrationDate { get; set; }

        public string address { get; set; }

        public string SaveAstudent(Student aStudent, List<StudentEducation> aStudentEducation,Department aDepartment)
        {
            bool unique = true;
            string registrationYear = RegistrationDate(registrationDate);
            string deptCode = aDepartment.DepartmentCode;
            string registrationNo = "";
            string randomNumber = "";
            while (unique)
            {
                Random random = new Random();
                int ran = random.Next(100,999);
                randomNumber = ran.ToString();
                registrationNo = registrationYear + deptCode + randomNumber;
                unique = manager.GetUniqueKey(registrationNo);
            }
            registrationNo = registrationYear + deptCode + randomNumber;
            return manager.SaveStudentInformation(registrationNo, aStudent, aStudentEducation);
        }

        private string RegistrationDate(string registrationDate)
        {
            string[] registrationYear = registrationDate.Split('/');
            return registrationYear[2];
        }

        public Student SearchAstudent(string regNo)
        {
            return manager.SearchAstudent(regNo);
        }

        public string EnrollAcourse(string registrationNo, Course aCourse)
        {
            return manager.EnrollAcourse(registrationNo, aCourse);
        }

        public List<Course> CompletedCourses(List<Course> allCourses)
        {
            List<Course> completedCourses = new List<Course>();
            foreach (Course aCourse in allCourses)
            {
                if (aCourse.ObtainedGradeLetter != "Unassigned")
                {
                    completedCourses.Add(aCourse);
                }
            }
            return completedCourses;
        }

        public double CountTotalCredit(List<Course> allCourses)
        {
            double totalCredit = 0;
            foreach (Course aCourse in allCourses)
            {
                if(aCourse.CourseCredit != null)
                {
                    totalCredit = totalCredit + aCourse.CourseCredit;
                }
            }
            return totalCredit;
        }

        public double CountCompletedCredit(List<Course> completedCourses)
        {
            double completedCredit = 0;
            foreach (Course aCourse in completedCourses)
            {

              completedCredit = completedCredit + aCourse.CourseCredit;
                
            }
            return completedCredit;
        }
    }
}